layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = layui.layer;

    getCaptcha('captchaPic');

    // 粒子线条背景
    $(document).ready(function () {
        $('.layui-container').particleground({
            dotColor: '#5cbdaa',
            lineColor: '#5cbdaa'
        });
        if (window != top) {
            top.location.href = location.href;
        }
    });
    $('#captchaPic').bind('click', function () {
        getCaptcha('captchaPic');
    });

    // 进行登录操作
    form.on('submit(login)', function (data) {
        data = data.field;
        if (data.username == '') {
            layer.msg('手机号不能为空');
            return false;
        }
        if (data.password == '') {
            layer.msg('密码不能为空');
            return false;
        }
        if (data.captcha == '') {
            layer.msg('验证码不能为空');
            return false;
        }
        var obj = {
            'username': data.username,
            'password': data.password,
            'captcha': data.captcha,
            'captcha_id': getCookie(cookie_captcha_name),
        };
        var _this = $(this);
        _this.text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
        $.ajax({
            type: "POST",
            url: login,
            data: obj,
            success: function (result) {
                var code = parseInt(result.code)
                if (code !== 0) {
                    layer.open({
                        skin: 'layui-layer-molv',
                        content: result.message,
                        end: function (index, layero) {
                            _this.text("登 录").attr("disabled", false).removeClass("layui-disabled");
                            if (code == 99) {
                                getCaptcha('captchaPic');
                            }
                            return false;
                        }
                    });
                    return false;
                }
                // var menus = JSON.stringify(result.data.menus);
                // localStorage.setItem('menus', menus);
                localStorage.setItem('user', JSON.stringify(result.data));
                window.location.href = "/";
            },
            complete: function( xhr,data ) {
                setToken(xhr.getResponseHeader('Authorization'));
            }
        });
        return false;
    });
});
